package edu.purdue.spm.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import edu.purdue.spm.Login;
import edu.purdue.spm.MainDashBoardFragment;
import edu.purdue.spm.R;
import edu.purdue.spm.ViewPagerFragmentActivity;
import edu.purdue.spm.entity.Task;
import edu.purdue.spm.util.Portal;
import edu.purdue.spm.util.StopWatch;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;

public class TimeLineFragment extends Fragment{
	static int dine = Color.parseColor("#BF00CC66");
	static int yellow = Color.parseColor("#BFCCCC00");
	static int purpleRed = Color.parseColor("#BFCC0066");
	static int gray = Color.parseColor("#BF606060");
	static int pink = Color.parseColor("#BFFF6666");
	static int purpleBlue = Color.parseColor("#BFCC00CC");
	static int sky = Color.parseColor("#BF0066CC");
	static int ocean = Color.parseColor("#BF0000CC");
	static int orange = Color.parseColor("#BFFF8000");
	static int earthYellow = Color.parseColor("#BFCC6600");
	static int green = Color.parseColor("#BF009900");
	static int red = Color.parseColor("#BFCC0000");
	static int grass = Color.parseColor("#BF66CC00");
	static int purple = Color.parseColor("#BF6600CC");

	public static int[] colorSpectrum = {yellow, purpleRed, gray, pink, purpleBlue, sky, ocean, orange, earthYellow, green, red, grass, purple};

	public static HashMap<String, Integer> task_color_bind = new HashMap<String, Integer>();

	static int width=ViewPagerFragmentActivity.screenWidth-30;
	static int height;

	public static void bindColorToSomeTasks(){
	//	Log.i("bindColorToSomeTasks", MainDashBoardFragment.fullTaskList+"");
		int i=0;

		for(Task task : MainDashBoardFragment.fullTaskList){
			if(task==null){
				continue;
			}
			if(Time.compare(task.getDueTime(), StopWatch.getTimeInTime()) > 0 && task.getDueTime().toMillis(true)<(StopWatch.getTimeInTime().toMillis(true)+604800000)){
				task_color_bind.put(task.getTaskID(), colorSpectrum[i%colorSpectrum.length]);
				i++;
			}else{
				task_color_bind.put(task.getTaskID(), dine);
			}
		}
	}




	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("MSG", "TimeLineFragment onCreateView !!!");
		bindColorToSomeTasks();
		View base = new SampleView(inflater.getContext());
		//		width = ((View) container.getParent()).findViewById(R.id.timeLine_container_layout).getWidth();
		//		height = ((View) container.getParent()).findViewById(R.id.timeLine_container_layout).getHeight();

		return base;
	}





	private static class SampleView extends View {

		public SampleView(Context context) {
			super(context);


		}
		@Override protected void onDraw(Canvas canvas) {

		//	Log.i("fullTaskList", MainDashBoardFragment.fullTaskList+"");
			bindColorToSomeTasks();
			canvas.drawColor(Color.parseColor("#BF330019"));
			Paint paint = new Paint();
			paint.setColor(Color.WHITE);
			canvas.drawLine(0, 55, width, 55, paint);
			canvas.drawLine(width, 55, width-30, 25, paint);
			canvas.drawLine(width, 55, width-30, 85, paint);

			long currTimeInLong = StopWatch.getTimeInTime().toMillis(true);
			paint.setColor(Color.parseColor("#BF009900"));

			int defultRadius = 20;


			for(Task task : MainDashBoardFragment.fullTaskList){
				if(Time.compare(task.getDueTime(), StopWatch.getTimeInTime()) > 0 && task.getDueTime().toMillis(true)<(currTimeInLong+604800000)){
					//	Log.i("task_color_bind.get(task.getTaskID())", ""+task_color_bind.get(task.getTaskID()));

					if(task_color_bind.get(task.getTaskID())==null){
					//	bindColorToSomeTasks();
					}
					paint.setColor(task_color_bind.get(task.getTaskID()));


					paint.setStyle(Paint.Style.FILL);
					canvas.drawCircle(width*(task.getDueTime().toMillis(true)-currTimeInLong)/604800000, 55, (float) (defultRadius*task.getProgress()), paint);
					paint.setStyle(Paint.Style.STROKE);
					canvas.drawCircle(width*(task.getDueTime().toMillis(true)-currTimeInLong)/604800000, 55, (float) (defultRadius), paint);
				}
			}

			invalidate();
		}
	}

}
